﻿using log4net;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMGoodsPriceView.xaml
  /// </summary>
  public partial class DMGoodsPriceView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMGoodsPriceView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public ObservableCollection<PriceModel> PriceList { get; set; }
    public PriceModel SelectedPrice { get; set; }
    public DMGoodsPriceView()
    {
      InitializeComponent();
      this.DataContext = this;

      PriceList = new ObservableCollection<PriceModel>(PriceModel.SelectPrice());

      ((GridViewComboBoxColumn)this.radGridView.Columns["Customer"]).ItemsSource = CustomerModel.SelectCustomerByType("采购");
      ((GridViewComboBoxColumn)this.radGridView.Columns["Goods"]).ItemsSource = GoodsModel.SelectValidGoods();
    }

    private void RadGridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
    {
      //做保存动作时先清空消息栏里之前的提示
      InfoBar = string.Empty;

      //通过【取消】结束编辑时，删掉选中的编辑行
      if (e.EditAction == GridViewEditAction.Cancel)
      {
        PriceList.Remove((PriceModel)e.EditedItem);
        return;
      }

      //结束编辑时，有错误，返回
      if (e.UserDefinedErrors.Count > 0)
      {
        return;
      }

      PriceModel price = e.Row.DataContext as PriceModel;

      //根据是否有AutoNo判断是新增还是更新
      if (price.AutoNo == null)
      {
        if (0 < PriceModel.InsertPrice((PriceModel)e.EditedItem))
        {
          //新增完成后重载列表，将数据库中自动生成的id也读取出来
          PriceList = new ObservableCollection<PriceModel>(PriceModel.SelectPrice());

          InfoBar = $"新增价格完成{price.CustomerId}-{price.GoodsId}-{price.Price}";
          log.Info(InfoBar);
        }
        else
        {
          InfoBar = $"新增价格失败";
        }
      }
      else
      {
        if (0 < PriceModel.UpdatePrice((PriceModel)e.EditedItem))
        {
          InfoBar = $"修改价格完成{price.CustomerId}-{price.GoodsId}-{price.Price}";

          log.Info(InfoBar);
        }
        else
        {
          InfoBar = $"修改价格失败";
        }
      }
    }

    private void RadGridView_RowValidating(object sender, GridViewRowValidatingEventArgs e)
    {
      PriceModel price = e.Row.DataContext as PriceModel;
      if (price.CustomerId == null)
      {
        GridViewCellValidationResult validationResult = new GridViewCellValidationResult();
        validationResult.PropertyName = "CustomerId";
        validationResult.ErrorMessage = "客户不能为空";
        e.ValidationResults.Add(validationResult);
        e.IsValid = false;
      }
      if (price.GoodsId == null)
      {
        GridViewCellValidationResult validationResult = new GridViewCellValidationResult();
        validationResult.PropertyName = "GoodsId";
        validationResult.ErrorMessage = "物资不能为空";
        e.ValidationResults.Add(validationResult);
        e.IsValid = false;
      }
      if (string.IsNullOrEmpty(price.Price))
      {
        GridViewCellValidationResult validationResult = new GridViewCellValidationResult();
        validationResult.PropertyName = "Price";
        validationResult.ErrorMessage = "价格不能为空";
        e.ValidationResults.Add(validationResult);
        e.IsValid = false;
      }
      if (0 < PriceModel.SelectExistPrice(price))
      {
        GridViewCellValidationResult validationResult = new GridViewCellValidationResult();
        validationResult.PropertyName = "Price";
        validationResult.ErrorMessage = "当前客户和物资对应的价格已存在";
        e.ValidationResults.Add(validationResult);
        e.IsValid = false;
      }
    }

    private void RadGridView_Deleting(object sender, GridViewDeletingEventArgs e)
    {
      foreach (PriceModel price in e.Items)
      {
        var ret = PriceModel.DeletePrice(price);
        if (ret > 0)
        {
          InfoBar = $"价格信息已删除";
          log.Info(InfoBar);
        }
        else if (ret < 0)
        {
          InfoBar = $"删除失败，请查看系统日志！";
        }
        return;
      }
    }

  }
}
